package org.study.redis.boot.component;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.study.redis.boot.model.*;

/**
 * 类的描述
 *
 * @author Administrator
 * @date 2021-06-21
 */
@Slf4j
@Component
public class RedisReceiver {
    private static class PushUtil {
        public static void pushOne(String tipMsg, String toUserId, String content) {
            log.info("【个性化推送】提示信息：{}，目标用戶：{}，具体内容：{}", tipMsg, toUserId, content);
        }
    }

    public void receiveCollect(String msg) {
        log.info("消费收藏数据：[{}]", msg);
        MessageCollect messageCollect = JSON.parseObject(msg, MessageCollect.class);
        PushUtil.pushOne("新消息", messageCollect.getToUserId(), messageCollect.getUserName() + "收藏了您的《" + messageCollect.getTitle() + "》作品");
    }

    public void receiveComment(String msg) {
        log.info("消费评论数据：[{}]", msg);
        MessageComment messageComment = JSON.parseObject(msg, MessageComment.class);
        PushUtil.pushOne("您有新的评论", messageComment.getToUserId(), messageComment.getUserName() + "对《" + messageComment.getTitle() + "》作品进行了评论：" + messageComment.getText());
    }

    public void receiveFocus(String msg) {
        log.info("消费关注数据：[{}]", msg);
        MessageFocus messageFocus = JSON.parseObject(msg, MessageFocus.class);
        PushUtil.pushOne("新消息", messageFocus.getToUserId(), messageFocus.getUserName() + "关注了您");
    }

    public void receivePraise(String msg) {
        log.info("消费点赞数据：[{}]", msg);
        MessagePraise messagePraise = JSON.parseObject(msg, MessagePraise.class);
        PushUtil.pushOne("新消息", messagePraise.getToUserId(), messagePraise.getUserName() + "点赞您的《" + messagePraise.getTitle() + "》作品");
    }

    public void webSocketTopicReceive(String msg) {
        log.info("webSocketTopic 收到数据：[{}]", msg);
        SubscribeDto subscribeDto = JSON.parseObject(msg, SubscribeDto.class);
        System.out.println(JSON.toJSONString(subscribeDto));
    }
}
